<?php

class RoleAction extends PublicAction
{
    // 角色管理列表
    public function index()
    {
        $AuthGroupDB = D('AuthGroup');

        $count = $AuthGroupDB->count();

        import('@.ORG.Page');
        $page = new Page($count, 15);
        $show = $page->show();
        $list = $AuthGroupDB->order('id ASC')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);// 赋值分页输出

        //记录当前位置
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->display();
    }

    // 添加角色
    public function add()
    {
        $AuthGroupDB = D("AuthGroup");
        if (IS_POST) {
            //根据表单提交的POST数据创建数据对象
            if ($AuthGroupDB->create()) {
                if ($AuthGroupDB->add()) {
                    $this->success('添加成功！', cookie('__forward__'));
                } else {
                    $this->error('添加失败!');
                }
            } else {
                $this->error($AuthGroupDB->getError());
            }
        } else {
            $this->assign('tpltitle', '添加');
            $this->display();
        }
    }

    // 编辑角色
    public function edit()
    {
        $AuthGroupDB = D("AuthGroup");
        if (IS_POST) {
            //根据表单提交的POST数据创建数据对象
            if ($AuthGroupDB->create()) {
                if ($AuthGroupDB->save()) {
                    $this->success('编辑成功！', cookie('__forward__'));
                } else {
                    $this->error('编辑失败!');
                }
            } else {
                $this->error($AuthGroupDB->getError());
            }
        } else {
            $id = I('id', 'intval', 0);
            if (!$id) $this->error('参数错误!');
            $info = $AuthGroupDB->find($id);
            $this->assign('info', $info);
            $this->display();
        }
    }

    //删除角色
    public function delete()
    {
        $id = I('id', 0, 'intval');
        if (!$id) $this->error('参数错误!');

        if ($id == 1) {
            $this->error('超级管理不允许删除!');
        }
        $AuthGroupDB = D('AuthGroup');
        if ($AuthGroupDB->delete($id)) {
            $this->assign("jumpUrl", U('AuthGroup/index'));
            $this->success('删除成功！');
        } else {
            $this->error('删除失败!');
        }
    }


    /* ========权限设置部分======== */
    //权限浏览
    public function access()
    {
        $rid = I('get.roleid', 0, 'intval');
        $authGroupData = M('AuthGroup')->where('id = ' . $rid)->find();

        if (IS_AJAX) {
            $result = M('AuthRule')->where("status=1")->select();

            $rules = explode(',', $authGroupData['rules']);
            foreach ($result as $k=>$v) {
                if (in_array($v['id'], $rules)) {
                    $result[$k]['checked'] = true;
                } else {
                    $result[$k]['checked'] = false;
                }
            }

            $this->ajaxReturn($result);
        }


        //记录当前位置
        cookie('location', $_SERVER['REQUEST_URI']);

        $this->assign('rules', $authGroupData['rules']);

        $this->assign('group_id', $rid);
        $this->display();
    }

    //权限编辑
    public function access_edit()
    {
        $group_id = $_POST['group_id'];


        if (!empty($group_id)) {
            $data['id'] = $group_id;
            $data['rules'] = $_POST['rules'];

            $r = M('AuthGroup')->save($data);
            if (false !== $r) {
                $this->success(L('role_ok'));
            } else {

                $this->error(L('role_error'));
            }
        } else {
            $this->error(L('do_empty'));
        }
    }
}